<template>

  <div class="app-container">
    <div class="filter-container">
      <div class="filter-container">
        <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入账号" v-model="listQuery.name"></el-input>
        <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </div>
    </div>
    <el-table :data="messageList" border style="width: 100%;" >
      <el-table-column prop="id" label="ID" >
      </el-table-column>
      <el-table-column prop="name" label="姓名" >
      </el-table-column>
      <el-table-column prop="email" label=邮箱>
      </el-table-column>
      <el-table-column prop="content" label="留言">
      </el-table-column>
      <el-table-column align="center" label="操作" width="260" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteMessage(scope.row)">删除</el-button>
          <!--<el-button size="mini" type="danger" @click="">回复</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import { fetchMessagerList, deleteMessageById } from '@/api/message'
  import waves from '@/directive/waves'
  export default {
    directives: {
      waves
    },
    data() {
      return {
        messageList: null,
        total: null,
        message: {
          id: null
        },
        listQuery: {
          page: 1,
          size: 10,
          name: undefined
        }
      }
    },
    created() {
      this.getMessageList()
    },
    methods: {
      getMessageList() {
        fetchMessagerList(this.listQuery).then(response => {
          this.messageList = response.data.rows
          this.total = response.data.total
        })
      },
      deleteMessage(row) {
        this.message.id = row.id
        this.$confirm('此操作将永久性删除，是否继续', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMessageById(this.message).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            const index = this.messageList.indexOf(row)
            this.messageList.splice(index, 1)
          })
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getMessageList()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getMessageList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getMessageList()
      }
    }
  }
</script>

<style scoped>
  .app-container{
    padding: 30px;
  }
</style>
